package com.datastructure.test.removeduplicatesletter;

public class RemoveDuplicatesLetter {

    public static void main(String[] args) {
        RemoveDuplicatesLetter removeDuplicatesLetter = new RemoveDuplicatesLetter();
        String str = "bcaaac";
        String removeDuplicates = removeDuplicatesLetter.removeDuplicates(str);
        System.out.println(removeDuplicates);
    }

    public String removeDuplicates (String s) {
        // write code here
        String temp=s;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length-1; i++) {
            if(chars[i]==chars[i+1]){
                temp=s.substring(0,i)+s.substring(i+2);
                temp=removeDuplicates(temp);
                break;
            }
        }
        return temp;
    }

    public String removeDuplicates1 (String s) {
        // write code here
        StringBuilder bu = new StringBuilder();
        char c = s.charAt(0);
        bu.append(c);
        int len = s.length();
        int index = 1;
        while (index < len) {
            if (bu.length() == 0 || bu.charAt(bu.length() - 1) != s.charAt(index)) {
                bu.append(s.charAt(index));
                index++;
            } else {

                bu.deleteCharAt(bu.length() - 1);
                index++;
            }
        }

        return bu.toString();
    }
}
